package com.hermit.qmst.UI.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hermit.qmst.R;
import com.hermit.qmst.UI.custom.GlideImageLoader;
import com.hermit.qmst.customView.Image3DSwitchView;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Preferences;
import com.hermit.qmst.tools.RequestHttp;
import com.hermit.qmst.tools.Utils;
import com.youth.banner.Banner;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;

import cn.bingoogolapple.bgabanner.BGABanner;


/**
 * Created by Administrator on 2016/12/31 0031.
 */
@ContentView(R.layout.new_activity_main)
public class MainActivity extends Activity implements View.OnClickListener, BGABanner.Adapter<ImageView, String>{
    @ViewInject(R.id.tv_title)
    TextView mTvTitle;

    @ViewInject(R.id.tv_yue)
    TextView mTvYue;

    @ViewInject(R.id.tv_ads_marquee)
    TextView mTvadsMarquee;

    @ViewInject(R.id.tv_name)
    TextView mTvName;

    Image3DSwitchView imageSwitchView;
    private final static long TIME_INTERVAL = 5000;
    @ViewInject(R.id.bt_tab1)
    Button mBtTab1;
    @ViewInject(R.id.bt_tab2)
    Button mBtTab2;
    @ViewInject(R.id.bt_tab3)
    Button mBtTab3;
    @ViewInject(R.id.iv_user)
    ImageView mIvUser;
    @ViewInject(R.id.iv_right)
    ImageView mIvRight;
    private Handler mHandler = new Mhandlder();
    private String TAG = "MainActivity";


    public static String appName = "";
    public static String thirdName = "";
    public static String money = "";
    /**
     * 点点数组
     */
    private ImageView[] tips;
    private String getUrl = "";
    private ArrayList<String> mImageUrl = new ArrayList<>();
    private ArrayList<String> mImages = new ArrayList<>();
    private Banner mBanner;
    //private BGABanner mBGABanner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mBanner = (Banner) findViewById(R.id.banner);
        mBanner.setImageLoader(new GlideImageLoader());
       // mBGABanner = (BGABanner) findViewById(R.id.banner_guide_content);
        mBtTab1.setOnClickListener(this);
        mBtTab2.setOnClickListener(this);
        mBtTab3.setOnClickListener(this);
        mIvUser.setOnClickListener(this);
        mIvRight.setOnClickListener(this);
        //mBGABanner.setAdapter(MainActivity.this);
        Preferences preferences = new Preferences(this);
        Common.myPhone = preferences.getPreferenceStringValue(Preferences.MY_PHONE);
        Common.myPassword = preferences.getPreferenceStringValue(Preferences.MY_PASSWORD);
        Common.mIsAutoAnswer = preferences.getPreferenceBooleanValue(Preferences.AUTO_RING);
        Common.mSipAccout = preferences.getPreferenceStringValue(preferences.SIP_ACCOUNT);
        //代理商
        Common.mAgentId = preferences.getPreferenceStringValue(Preferences.SIP_AGENT);
        Common.mAgentThirdId = preferences.getPreferenceStringValue(Preferences.SIP_AGENT_THIRD);
        Common.mUserId = preferences.getPreferenceStringValue(Preferences.USER_ID);
        getUrl = Common.mUrl + "szdh/index.php?uid=" + Common.mUserId + "&agentId=" + Common.mAgentId + "&agent_id_third=" + Common.mAgentThirdId;
        initData();
        mTvTitle.setText("全民商通4.0");
        mTvName.setText("---------- 全民商通4.0 ----------");
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.bt_tab1:
                intent.setClass(MainActivity.this, com.hermit.qmst.MainActivity.class);
                break;
            case R.id.bt_tab2:
                BottleActivity.type = "official";
                intent.setClass(MainActivity.this, BottleActivity.class);
                break;
            case R.id.bt_tab3:
                intent.setClass(MainActivity.this, FindActivity.class);
                break;
            case R.id.iv_user:
                intent.setClass(MainActivity.this, UserInfoActivity.class);
                break;
            case R.id.iv_right:
                BottleActivity.type = "drift";
                intent.setClass(MainActivity.this, BottleActivity.class);
                break;
        }
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        imageSwitchView.clear();
    }

    private void initData() {
        RequestHttp.getHttp(MainActivity.this, getUrl, new RequestHttp.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject) {
                try {
                    if (jsonObject.getBoolean("state")) {
                        JSONArray jsonArray = jsonObject.getJSONArray("imgs");

                        if (jsonArray.length() > 0) {
                            mImageUrl.clear();
                            mImages.clear();
                            for (int i = 0; i < jsonArray.length(); i++) {
                                mImageUrl.add(jsonArray.getJSONObject(i).getString("img"));
                                mImages.add(jsonArray.getJSONObject(i).getString("url"));
                            }
                        }
                        android.util.Log.d("MainActivity", mImageUrl.get(0));
                        android.util.Log.d("MainActivity", mImageUrl.get(1));
                        android.util.Log.d("MainActivity", mImageUrl.get(2));
                        //mBGABanner.setData(mImageUrl,null);
                        mBanner.setImageLoader(new GlideImageLoader());
                        mBanner.setImages(mImageUrl).setImageLoader(new GlideImageLoader()).start();
                        appName = jsonObject.getString("name");
                        money = Utils.stringToInt(jsonObject.getString("money"));
                        mTvYue.setText("余额:" + money);
                        mTvadsMarquee.setText(jsonObject.getString("count"));
                        thirdName = jsonObject.getString("thirdName");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
        Glide.with(MainActivity.this)
                .load(model)
                .placeholder(R.drawable.holder)
                .error(R.drawable.holder)
                .centerCrop()
                .dontAnimate()
                .into(itemView);
    }

    class Mhandlder extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 2) {
                imageSwitchView.scrollToPrevious();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        initData();
    }
}
